﻿#########################################################################################################
####	Author: Rahul G. Babar																		 ####
####    Purpose: Use this PowerShell script to upload administrator approved, infopath forms with    ####
####             code behind in central admin form templates and activate it on a particular site    ####
####             collection. The script will deactivate and uninstall the form template first if it  ####
####             already exists. Make sure that the form template is kept in same directory          ####
####             of script file.                                                                     ####
####             Edit the script for specific requriements. 						     		     ####
#########################################################################################################

function LoadSharePointPowerShellEnvironment
{	
	write-host "Setting up PowerShell environment for SharePoint" -foregroundcolor Yellow
	write-host 
	Add-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue
	write-host "SharePoint PowerShell Snapin loaded." -foregroundcolor Green
	write-host 
}

function Get-ScriptDirectory
{
	$Invocation = (Get-Variable MyInvocation -Scope 1).Value
	Split-Path $Invocation.MyCommand.Path
}

LoadSharePointPowerShellEnvironment
$siteUrl = $null
$infopathFormName = $null
$infopathFeatureId = $null
$infopathFormFeature = $null
$infopathFormTemplate = $null
$AdminServiceName = "SPAdminV4"   
$WasAdminServiceRunning = $true

# Start SharePoint Administration Service if it's not already started.
if ($(Get-Service $AdminServiceName).Status -eq "Stopped")   
{   
	Write-Host "Starting SharePoint Administration Service since it is not already started..." -ForegroundColor Yellow
	Write-Host

    $WasAdminServiceRunning = $false;   
    Start-Service $AdminServiceName

	Write-Host "SharePoint Administration Service Started." -ForegroundColor Green
	Write-Host
} 

$siteUrl = Read-Host "Enter site collection Url: "

if($siteUrl -ne $null -and $webUrl -ne "")
{
	$siteCollection = Get-SPSite -Identity $siteUrl -ErrorAction SilentlyContinue
	if($siteCollection -ne $null)
	{
		$infopathFormName = Read-Host "Enter infopath form template name (ex. Form.xsn): "
		if($infopathFormName -ne $null -and $infopathFormName -ne "")
		{
			$infopathFormTemplate = Get-SPInfoPathFormTemplate -Identity $infopathFormName -ErrorAction SilentlyContinue
			if($infopathFormTemplate -ne $null)
			{				
				$infopathFeatureId = $infopathFormTemplate.FeatureId
				$infopathFormFeature = Get-SPFeature -Site $siteCollection | Where-object {$_.Id -eq $infopathFeatureId}	-ErrorAction SilentlyContinue	
				if($infopathFormFeature -ne $null)
				{
					Write-Host "Infopath form template is already activated at site collection. Deactivating it..." -ForegroundColor Yellow
					Write-Host	
					Disable-SPFeature -Identity $infopathFeatureId -Url $siteUrl -Confirm:$false
					
					while($infopathFormFeature -ne $null)
					{
						Start-Sleep 2
						$infopathFormFeature = Get-SPFeature -Site $siteCollection | Where-object {$_.Id -eq $infopathFeatureId}	-ErrorAction SilentlyContinue	
					}
					
					if($infopathFormFeature -eq $null)
					{
						Write-Host "Infopath form template is successfully deactivated from site collection." -ForegroundColor Green
						Write-Host
					}
					else
					{
						Write-Host "Could not deactivate form template from site collection." -ForegroundColor Red
						Write-Host
					}
					
					Write-Host "Uninstalling InfoPath form template..." -ForegroundColor Yellow
					Write-Host
					
					Uninstall-SPInfopathFormTemplate -Identity $infopathFormName -Confirm:$false -ErrorAction SilentlyContinue		
					
					while($infopathFormTemplate -ne $null)
					{
						Start-Sleep 2 	
						$infopathFormTemplate = Get-SPInfoPathFormTemplate -Identity $infopathFormName -ErrorAction SilentlyContinue
					}
					
					if($infopathFormTemplate -eq $null)
					{
						Write-Host "Infopath form template is uninstalled successfully." -ForegroundColor Green	
						Write-Host 
					}
					else
					{
						Write-Host "Could not uninstall infopath form template." -ForegroundColor Red
						Write-Host
					}
				}											
			}	
			Write-Host "Installing InfoPath form template..." -ForegroundColor Yellow
			Write-Host
			
			$path = Get-ScriptDirectory 
			$formPath = $path + "\" + $infopathFormName			
			Install-SPInfopathFormTemplate -Path $formPath -Confirm:$false -ErrorAction SilentlyContinue
			$infopathFormTemplate = $null;
			while($infopathFormTemplate -eq $null)
			{
				$infopathFormTemplate = Get-SPInfoPathFormTemplate -Identity $infopathFormName -ErrorAction SilentlyContinue
				Start-Sleep 2
			}	
			if($infopathFormTemplate -ne $null)
			{
				Write-Host "Infopath form template is installed successfully." -ForegroundColor Green
				Write-Host
							
				Write-Host "Activating Infopath form template on site collection..." -ForegroundColor Yellow
				Enable-SPFeature -Identity $infopathFormTemplate.FeatureId -Url $siteUrl -Confirm:$false -ErrorAction SilentlyContinue
				$infopathFormFeature = $null;
				while($infopathFormFeature -eq $null)
				{
					Start-Sleep 2
					$infopathFormFeature = Get-SPFeature -Site $siteCollection | Where-object {$_.Id -eq $infopathFeatureId} -ErrorAction SilentlyContinue	
				}
				if($infopathFormFeature -ne $null)
				{
					Write-Host "Infopath form template is successfully activated on site collection." -ForegroundColor Green					
				}
				else
				{
					Write-Host "Could not activate form template at site collection." -ForegroundColor Red					
				}
				Write-Host
			}
			else
			{
				Write-Host "Could not install infopath form template." -ForegroundColor Red
				Write-Host
			}
		}
		else
		{
			Write-Host "Invalid infopath form template name." -ForegroundColor Red
			Write-Host
		}
	}
	else
	{
		Write-Host "Could not find site collection at $siteUrl." -ForegroundColor Red
		Write-Host
	}
}
else
{
	Write-Host "Invalid site collection url." -ForegroundColor Red
	Write-Host
}

# Stop SharePoint Administration Service if it was not already started.
if($WasAdminServiceRunning -ne $true)
{
	Write-Host "Stopping SharePoint Administration Service..." -ForegroundColor Yellow
	Write-Host
	
	Stop-Service $AdminServiceName
	
	Write-Host "SharePoint Administration Service Stopped." -ForegroundColor Green
	Write-Host
}

